package com.rabbitmq.service.impl;

import javax.annotation.PostConstruct;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rabbitmq.service.RabbitMqService;
import com.rabbitmqConfig.RabbitMqConfig;

@Service("rabbitmqService")
public class RabbitMqServiceImpl implements RabbitMqService,RabbitTemplate.ConfirmCallback ,RabbitTemplate.ReturnCallback{
	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	@PostConstruct
	public void init() {
		rabbitTemplate.setConfirmCallback(this);
		rabbitTemplate.setReturnCallback(this);
	}
	@Autowired
	private AmqpTemplate amqpTemplate;
	/**
	 * 一个交换机对应一个队列，可添加多个消费者
	 */
	@Override
	public void sendMessage(String message) {
		amqpTemplate.convertAndSend(RabbitMqConfig.DIRECT_EXCHANGE, RabbitMqConfig.routingKey, message);

	}
	/**
	 * 一个交换机对应多个队列，实现发布订阅模式
	 */
	@Override
	public void sendMessage2(String message) {
		amqpTemplate.convertAndSend(RabbitMqConfig.FANOUT_EXCHANGE, "", message);

		
	}
	@Override
	public void sendRoutingMessage(String routingKey,String message) {
		amqpTemplate.convertAndSend(RabbitMqConfig.DIRECT_ROUTING_EXCHANGE, routingKey,message );

	}
	@Override
	public void sendRoutingMessage() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void sendTopicMessage(String routingKey, String message) {
		CorrelationData correlationData = new CorrelationData("local"+System.currentTimeMillis()+"");
		rabbitTemplate.convertAndSend(RabbitMqConfig.TOPIC_EXCHANGE+"1", routingKey,message ,correlationData);
		
	}
	/**
	 *  消息发送状态确认，添加确认
	 * 1. 设置publisher-confirm-type: correlated
	 * 2. 实现 RabbitTemplate.ConfirmCallback 接口
	 * 2. 设置 rabbitTemplate.setConfirmCallback(this);开启
	 * correlationData 消息主键 需要在发送消息是主动添加
	 * ack  是否成功
	 * cause 失败原因
	 */
	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		System.out.println("confirm=======:"+correlationData);
		System.out.println("ack=======:"+ack);
		System.out.println("cause=======:"+cause);
		
	}
	@Override
	/**
	 * 消息回退模式，
	 * 1. 设置 publisher-returns: true
	 * 2. 实现 RabbitTemplate.ReturnCallback 接口
	 * 2. 设置 rabbitTemplate.setReturnCallback(this);开启
	 * 	message the returned message.
	 * 	replyCode the reply code.
	 * 	replyText the reply text.
	 * 	exchange the exchange.
	 * 	routingKey the routing key.
	 */
	public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
		System.out.println("returnedMessage Message:"+message);
		System.out.println("returnedMessage replyCode:"+replyCode);
		System.out.println("returnedMessage replyText:"+replyText);
		System.out.println("returnedMessage exchange:"+exchange);
		System.out.println("returnedMessage routingKey:"+routingKey);
		
	}

}
